import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_application_1/pages/message_page/message_view.dart';
import 'package:flutter_application_1/pages/my_page/my_view.dart';
import 'package:flutter_application_1/pages/other_page/other_view.dart';
import 'package:get/get.dart';

import 'home_logic.dart';

class HomePage extends StatelessWidget {
  HomePage({Key? key}) : super(key: key);

  final HomeLogic logic = Get.put(HomeLogic());

  @override
  Widget build(BuildContext context) {
    final tabs = [
      const _HomeTab(), // 首页
      OtherPage(),
      MessagePage(),
      MyPage(),
    ];
    return Obx(
      () => Scaffold(
        body: IndexedStack(index: logic.currentTab.value, children: tabs),
        bottomNavigationBar: BottomNavigationBar(
          currentIndex: logic.currentTab.value,
          type: BottomNavigationBarType.fixed,
          selectedItemColor: Colors.blue,
          unselectedItemColor: Colors.grey,
          onTap: logic.switchTab,
          items: [
            BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
            BottomNavigationBarItem(
              icon: Icon(Icons.account_box_rounded),
              label: 'Other',
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.message),
              label: 'message',
            ),
            BottomNavigationBarItem(icon: Icon(Icons.person), label: 'My'),
          ],
        ),
      ),
    );
  }
}

class _HomeTab extends StatelessWidget {
  const _HomeTab();

  @override
  Widget build(BuildContext context) {
    final logic = Get.find<HomeLogic>();
    return Obx(() {
      return Scaffold(
        appBar: AppBar(title: Text('首页')),
        body: CustomScrollView(
          slivers: [
            // 轮播
            SliverToBoxAdapter(
              child: Padding(
                padding: const EdgeInsets.only(top: 12, bottom: 8),
                child: CarouselSlider(
                  options: CarouselOptions(
                    height: 180,
                    autoPlay: true,
                    viewportFraction: 0.92,
                    enlargeCenterPage: true,
                  ),
                  items:
                      logic.banners.map((u) {
                        return ClipRRect(
                          borderRadius: BorderRadius.circular(12),
                          child: Image.network(
                            u,
                            fit: BoxFit.cover,
                            width: double.infinity,
                          ),
                        );
                      }).toList(),
                ),
              ),
            ),
            // 四个入口
            SliverToBoxAdapter(
              child: Padding(
                padding: const EdgeInsets.symmetric(
                  horizontal: 12,
                  vertical: 8,
                ),
                child: GridView.builder(
                  itemCount: logic.actions.length,
                  shrinkWrap: true,
                  physics: const NeverScrollableScrollPhysics(),
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 4,
                    mainAxisSpacing: 12,
                    crossAxisSpacing: 12,
                    childAspectRatio: .95,
                  ),
                  itemBuilder: (_, i) {
                    final a = logic.actions[i];
                    return InkWell(
                      onTap: () => Get.snackbar('点击', a['label'] as String),
                      borderRadius: BorderRadius.circular(12),
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          CircleAvatar(
                            radius: 24,
                            child: Icon(
                              IconData(
                                a['icon'] as int,
                                fontFamily: 'MaterialIcons',
                              ),
                            ),
                          ),
                          const SizedBox(height: 6),
                          Text(
                            a['label'] as String,
                            style: const TextStyle(fontSize: 12),
                          ),
                        ],
                      ),
                    );
                  },
                ),
              ),
            ),
          ],
        ),
      );
    });
  }
}
